package com.qidian.hw.dao.impl;

import com.qidian.hw.dao.SolutionTypeDAO;
import com.qidian.hw.pojo.SolutionType;
import com.qidian.hw.tools.FormatUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Author ꒰ঌ(李ᆺ琛)໒꒱
 * @Date 2022/8/19 18:20
 * @Version 1.8
 */
public class SolutionTypeDAOImpl extends BaseDAO implements SolutionTypeDAO {
    private Connection con;
    private PreparedStatement pstmt;
    private ResultSet rs;

    @Override
    public int saveSolutionType(SolutionType solutionType) {
        int result = 0;
        String sql = "insert into solution_type (solution_type_title,create_time,modify_time) values(?,?,?)";
        try {
            con = getCon ();
            pstmt = con.prepareStatement (sql);
            pstmt.setString (1, solutionType.getSolutionTypeTitle ());
            String dateStr = FormatUtil.getNow ();//获取当前系统时间
            pstmt.setString (2, dateStr);
            pstmt.setString (3, dateStr);
            result = pstmt.executeUpdate ();
        } catch (SQLException throwables) {
            throwables.printStackTrace ();
        } finally {
            closeAll (rs, pstmt, con);
        }
        return result;
    }

    @Override
    public int deleteSolutionType(long solutionTypeId) {
        int result = 0;
        String sql = "update solution_type set status=0,modify_time=? where solution_type_id = ?";
        try {
            con = getCon ();
            pstmt = con.prepareStatement (sql);
            pstmt.setString (1, FormatUtil.getNow ());
            pstmt.setLong (2, solutionTypeId);
            result = pstmt.executeUpdate ();
        } catch (SQLException throwables) {
            throwables.printStackTrace ();
        } finally {
            closeAll (rs, pstmt, con);
        }
        return result;
    }

    @Override
    public int updateSolutionType(SolutionType solutionType) {
        int result = 0;
        String sql = "update solution_type set solution_type_title = ?,modify_time = ? where solution_type_id = ?";
        try {
            con = getCon ();
            pstmt = con.prepareStatement (sql);
            pstmt.setString (1, solutionType.getSolutionTypeTitle ());
            pstmt.setString (2, FormatUtil.getNow ());
            pstmt.setLong (3, solutionType.getSolutionTypeId ());
            result = pstmt.executeUpdate ();
        } catch (SQLException throwables) {
            throwables.printStackTrace ();
        } finally {
            closeAll (rs, pstmt, con);
        }
        return result;
    }

    @Override
    public ArrayList<SolutionType> listSolutionType() {
        ArrayList<SolutionType> solutionTypes = new ArrayList<> ();
        String sql = "select * from solution_type where status = 1";
        try {
            con = getCon ();
            pstmt = con.prepareStatement (sql);
            rs = pstmt.executeQuery ();
            while (rs.next ()) {
                SolutionType solutionType = new SolutionType ();
                solutionType.setSolutionTypeId (rs.getLong ("solution_type_id"));
                solutionType.setSolutionTypeTitle (rs.getString ("solution_type_title"));
                solutionType.setCreateTime (rs.getString ("create_time"));
                solutionType.setModifyTime (rs.getString ("modify_time"));
                solutionTypes.add(solutionType);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace ();
        } finally {
            closeAll (rs, pstmt, con);
        }
        return solutionTypes;
    }

    @Override
    public SolutionType querySolutionTypeById(long solutionTypeId) {
        SolutionType solutionType = null;
        String sql = "select * from solution_type where solution_type_id = ? and status = 1";
        try {
            con = getCon ();
            pstmt = con.prepareStatement (sql);
            pstmt.setLong (1, solutionTypeId);
            rs = pstmt.executeQuery ();
            if (rs.next ()) {
                solutionType = new SolutionType ();
                solutionType.setSolutionTypeId (rs.getLong ("solution_type_id"));
                solutionType.setSolutionTypeTitle (rs.getString ("solution_type_title"));
                solutionType.setCreateTime (rs.getString ("create_time"));
                solutionType.setModifyTime (rs.getString ("modify_time"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace ();
        } finally {
            closeAll (rs, pstmt, con);
        }
        return solutionType;
    }
    @Override
    public int deleteBatch(String[] ids) {
        int result = 0;
        String sql = "update solution_type set status=0,modify_time=? where solution_type_id in (";
        for(int i = 0; i <ids.length;i++) {
            sql += ids[i]+",";
        }
        sql += "0)";
        try {
            con = getCon ();
            pstmt = con.prepareStatement (sql);
            pstmt.setString (1, FormatUtil.getNow ());
            result = pstmt.executeUpdate ();
        } catch (SQLException throwables) {
            throwables.printStackTrace ();
        } finally {
            closeAll (rs, pstmt, con);
        }
        return result;
    }
}
